
local M={}

local H0=60*60
local M0=60
local RunService = game:GetService("RunService")

function M.Play(probability,luck)

end

function M.RandomLucky(probability,luck)

    luck=luck or 0

    local sum=0
    
	for k,p in pairs(probability) do
		sum=sum+p+luck
	end

    local a=math.random(0,sum)

    local s=0
	for k,p in pairs(probability) do
        s=s+p+luck
		if a<=s then
            return k
        end
	end

	return 1
end

function M.GetProbabilityShowPercent(data)
    local sum=0
    for _,v in pairs(data) do
        sum=sum+v
    end
    local Percent={}
    for k,v in pairs(data) do
        Percent[k]=(v*100)/sum
    end
    return Percent
end

function M.secondsToHHMM(time)
    local h=math.floor(time/H0)
    local m= math.floor((time-h*H0)/M0)
    local s=time-h*H0-m*M0

    local str=""
    if h>0 then
        if h<10 then
            str=str.."0"..h..":"
        else
            str=str..h..":"
        end
      
    end

    if m<10 then
        str=str.."0"..m..":"
    else
        str=str..m..":"
    end

    if s<10 then
        str=str.."0"..s
    else
        str=str..s
    end

    return str
end


-- 格式解析:?(位数)_aaa(前3位)_yyyyy(标记位)
local Tag_YYYYY="11111"
local Tag_YYYYY_Lenght=#Tag_YYYYY
--获取标记位
function GetTag()
	return Tag_YYYYY
end
--对排行榜数值进行简短处理
function M.ShortRankNumber(num)
	if num <1000 then -- 小于三位数直接返回
		return num
	end
	local numInt=math.floor(num) --去除小数点
	local numStr=tostring(numInt)
	local digit,first2Digit
	if string.find(numStr,"e+") then --如果已经达到科学计数
		local list=string.split(numStr,"e+") --切割
		digit=tonumber(list[2])+1--计算位数
		first2Digit=tostring(math.floor(list[1]*100)) --取前三位
	else
		digit=#numStr --获取位数
		first2Digit = string.sub(tostring(numStr), 1, 3)--获取前3位
	end

	return tonumber(digit..first2Digit..GetTag()) --拼接为新数
end

function TagDel(num)

    local str=tostring(num)
	if string.find(str,Tag_YYYYY)  then
		local list=string.split(str,Tag_YYYYY)
		return list[1],true
	else
		return str
	end
end

--对排行榜数值进行恢复
function M.RecoverRankNumber(num)
	if num <1000 then -- 小于三位数直接返回
		return num
	end

	local numStr,tag=TagDel(num) --判断数值是否含有标记位
    if not tag then-- 没有标记，直接返回
        return num,true
    end
	local digit=#numStr --获取位数
    if digit<4 then --?位数_aaa 必然>3
        return num
    end
	local a1 = string.sub(tostring(numStr), digit-2)--获取aaa三位数
	local a2=string.sub(tostring(numStr), 1, digit-3)--获取位数
    a1= tonumber(a1)
    a2= tonumber(a2)
	for i=1,a2-3 do
		a1=a1*10
	end
	return a1
end


function M.TypeWriterEffect(textLabel,text,Delay)
    local delay = 0.2
    if Delay then
        delay = Delay
    end
    local elapsedTime = 0
    local i =0
    local connection = nil
    connection = RunService.Heartbeat:Connect(function(step)
        if  elapsedTime < delay  then
            elapsedTime = elapsedTime + step
        else
            i = i+1
            if i>#text then
                connection:Disconnect()
                return
            end
            textLabel.Text = text:sub(1, i)
            elapsedTime = 0
        end
    end)

end

--判断是否自己碰到了某个物体
function M.IsMeTouchEvent(part,name)
    local partName=name or "HumanoidRootPart"
    if part.Name~= partName then
        return false
    end
    local character= part:FindFirstAncestorOfClass("Model")
    if character then
        local player=game.Players:GetPlayerFromCharacter(character)
        if player and  player==game.Players.LocalPlayer then
           return true
        end
    end
    return false
end

return M